// gfx_SDL.c

#include "SDL.h"
#include "gfx_sdl.h"

void SDLKeyDown( SDLKey key )
{
	switch( key )
	{
		case SDLK_LEFT:
			inputManager.KeyDown( inputManager.cControllerLeft );
		break;

		case SDLK_RIGHT:
			inputManager.KeyDown( inputManager.cControllerRight );
		break;

		case SDLK_UP:
			inputManager.KeyDown( inputManager.cControllerUp );
		break;

		case SDLK_DOWN:
			inputManager.KeyDown( inputManager.cControllerDown );
		break;

		case SDLK_a:
			inputManager.KeyDown( inputManager.cControllerA );
		break;

		case SDLK_b:
			inputManager.KeyDown( inputManager.cControllerB );
		break;

		case SDLK_ESCAPE:case SDLK_q:
			inputManager.KeyDown( inputManager.cControllerBack );
		break;

		case SDLK_RETURN:
			inputManager.KeyDown( inputManager.cControllerStart );
		break;

		default:
			//Do Nawt
		break;
	}
	
};

void SDLKeyUp( SDLKey key )
{
	
	switch ( key )
	{
		case SDLK_LEFT:
			inputManager.KeyUp( inputManager.cControllerLeft );
		break;

		case SDLK_RIGHT:
			inputManager.KeyUp( inputManager.cControllerRight );
		break;

		case SDLK_UP:
			inputManager.KeyUp( inputManager.cControllerUp );
		break;

		case SDLK_DOWN:
			inputManager.KeyUp( inputManager.cControllerDown );
		break;

		case SDLK_a:
			inputManager.KeyUp( inputManager.cControllerA );
		break;

		case SDLK_b:
			inputManager.KeyUp( inputManager.cControllerB );
		break;

		case SDLK_ESCAPE:case SDLK_q:
			inputManager.KeyUp( inputManager.cControllerBack );
		break;

		case SDLK_RETURN:
			inputManager.KeyUp( inputManager.cControllerStart );
		break;

		default:
			// Do nothing
		break;
	}
	
};
namespace Skitchin_SDL
{
	int CDrawingInterface::blit( CAbstractDrawable * drawable )
	{
		 int func_return;
	     
		 SDL_Rect sourceRect = { drawable->GetU( ), drawable->GetV( ), drawable->GetW( ), drawable->GetH( ) };
		 SDL_Rect destRect = { (Sint16)drawable->GetX( ), (Sint16)drawable->GetY( ), drawable->GetW( ), drawable->GetH( ) };
		 SDL_Surface * theTexture = (SDL_Surface*)drawable->GetTexturePointer( );
	     
		 func_return = SDL_BlitSurface( theTexture, &sourceRect, screen, &destRect );
		 //func_return = SDL_BlitSurface( theTexture, &drawable->rctSourceCoords, screen, &drawable->rctDestCoords );
		 /*
			"If the blit is successful, it returns 0, otherwise it returns -1."
	     
			If either of the surfaces were in video memory, and the blit returns -2, the video memory was lost, so it should be reloaded with artwork and re-blitted:       
		 */
	     
		 if ( func_return < 0 )
		 {
			   return -1; // failure
		 }
		 else
		 {
			   return 0; // success
		 }
	     
	};

	void CDrawingInterface::destroywindow( )
	{
		 SDL_Quit( );
	};

	void CDrawingInterface::fillrect( SRGB color, SRect rectarea )
	{
		 Uint32 sdlcolor;
		 SDL_Rect sdlrect;
	     
		 sdlrect.x = (Sint16)rectarea.x; sdlrect.y = (Sint16)rectarea.y; sdlrect.w = rectarea.w; sdlrect.h = rectarea.h;
	     
		 sdlcolor = SDL_MapRGB( screen->format, color.r, color.g, color.b );
		 SDL_FillRect( screen, &sdlrect, sdlcolor);
	};

	void CDrawingInterface::fillscreen( SRGB color )
	{
		 Uint32 sdlcolor;
	     
		 sdlcolor = SDL_MapRGB( screen->format, color.r, color.g, color.b );
		 SDL_FillRect( screen, NULL, sdlcolor);
	};

	void CDrawingInterface::initwindow( unsigned int w, unsigned int h, unsigned int bpp, const char * title )
	{   
		if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
		{
			exit( 1 );
		}
	    
		screen = SDL_SetVideoMode( w, h, bpp, SDL_DOUBLEBUF );
		if ( screen == NULL )
		{
			exit( 2 );
		}
		SDL_WM_SetCaption( title, NULL );
	};

	void CDrawingInterface::swapbuffers( )
	{
		 SDL_Flip( screen );     
	};

	int CDrawingInterface::Tick( )
	{
		if( bDoubleBuffer )
			swapbuffers( );

		this->CalcFPS( );

		if ( iFPSLimit > 0 )
			this->ThrottleFPS( );

		if( SDL_PollEvent( &event ) )
		{
			switch (event.type)
			{
				case SDL_KEYDOWN:
					SDLKeyDown( event.key.keysym.sym ); // Pass key ID to 
				break;

				case SDL_KEYUP:
					SDLKeyUp( event.key.keysym.sym );
				break;

				case SDL_QUIT:
					return -1;
				break;
			}
		}
	
		return 0;
	}

	int CDrawable::TextureFromFile( char * file, SRGB alphacolor )
	{
		SDL_Surface * temp = NULL;
		Uint32 theColor;
	    
		char * path = new char[256];

		strcpy(path, "assets/");
		strcat(path, file);

		temp = SDL_LoadBMP( path );
	    
		if ( temp != NULL )
		{
			 temp = SDL_DisplayFormat( temp );
			 theColor = SDL_MapRGB( temp->format, alphacolor.r, alphacolor.g, alphacolor.b );
			 SDL_SetColorKey( temp, SDL_SRCCOLORKEY, theColor );

			 this->SetW(temp->w);
			 this->SetH(temp->h);

			 this->texturedata = temp;
			 this->texturefilename = new char[strlen(path)];
			 strcpy(this->texturefilename, path);
			 return 0;
		}// Only errors proceed beyond this point.

		printf("ERROR loading file: %s!\n", path);

	    
		SDL_FreeSurface( temp );
		texturedata = NULL;
	    
		return -1; // error
	};
}